package com.icebartech.base.common.util;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.icebartech.core.components.RedisComponent;
import com.icebartech.core.constants.IcebartechConstants;

/**
 * 各种id
 * @author wenhsh
 *
 */
@Component
public class IdGenerator {
	
	@Autowired
	private RedisComponent redisComponent;

	private final static String CODE_KEY = "Code";
	
	/**
	 * 获取流水号id
	 * @return
	 */
	public String getSeqId(){
		String rtnStr = "";
		long orderId = redisComponent.incRedisKey(IcebartechConstants.ORDER_ID_KEY, "seq_id");
		if (100000000 > orderId) {
			orderId = redisComponent.incRedisKey(IcebartechConstants.ORDER_ID_KEY, "seq_id", 100000000);
		}
		//生成4位数字随机数
		String randomNum = RandomStringUtils.randomNumeric(4);
		rtnStr += orderId + randomNum;
		return rtnStr;
	}
	
	/**
	 * 获取订单id
	 * @return
	 */
	public String getOrderId(){
		String rtnStr = "";
		//String date = DateTimeUtility.dateofnowonlynumber().substring(0, 4);
		long orderId = redisComponent.incRedisKey(IcebartechConstants.ORDER_ID_KEY, "order_id");
		if (1499937180 > orderId) {
			orderId = redisComponent.incRedisKey(IcebartechConstants.ORDER_ID_KEY, "order_id", 1499937180);
		}
		//生成4位数字随机数
		String randomNum = RandomStringUtils.randomNumeric(4);
		rtnStr += orderId + randomNum;
		return rtnStr;
	}
	
	/**
	 * 获取推荐码
	 * @return
	 */
	public String getCode(){
		String rtnStr = "";
		long code = redisComponent.incRedisKey(CODE_KEY, "code_id");
		if(code < 1000){
			code = redisComponent.incRedisKey(CODE_KEY, "code_id", 1000);
		}
		rtnStr = String.valueOf(code);
		return rtnStr;
	}
	
	/**
	 * 生成商品编号
	 * @return
	 */
	public String getProductCode(){
		String rtnStr = "";
		long code = redisComponent.incRedisKey(CODE_KEY, "product_code");
		if(code < 10000000){
			code = redisComponent.incRedisKey(CODE_KEY, "product_code", 10000000);
		}
		rtnStr = "P" + String.valueOf(code);
		return rtnStr;
	}
}
